﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Business_approveschedual : System.Web.UI.Page
{
    PM.PMBusiness.approveschedual approve = new PM.PMBusiness.approveschedual();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindddl();//绑定下拉框
        }
        //读取所有框信息
        getPanelInfo();
    }

    protected void bindddl()
    {
        string nowdate = DateTime.Now.ToString();
        string sqlstr="select plancode from Bus_ProjectPlan where status=0 and pstatus='已完成' and closedate<='"+nowdate+"'";
        PM.Code.Web.controlbind.BindDropDownList(this.ddlApprove, sqlstr, "plancode", "plancode");
        this.ddlApprove.Items.Insert(0, new ListItem("可选的项目编号", "99"));
        this.ddlApprove.SelectedIndex = 0;
    }
    /// <summary>
    /// 获取div中信息
    /// </summary>
    protected void getPanelInfo()
    {
        string innerHtml = approve.getProjectPlan(DateTime.Now.ToString());
        string[] array = innerHtml.Split(new char[1] { '?' });
        //string[] array = innerHtml.Split(new char[1] { '?' },System.StringSplitOptions.None);
        this.recentOpen.InnerHtml = array[0].ToString();
        this.recentClose.InnerHtml = array[1].ToString();
        this.nowRunning.InnerHtml = array[2].ToString();
    }

    //提交审批计划
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if(ddlApprove.SelectedIndex==0)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel3, "未选择评审对象！");
            return;
        }
        if (this.txtOpenDate.Value.Trim() == string.Empty)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel3, "开启时间未填写！");
            return;
        }
        if (this.txtCloseDate.Value.Trim() == string.Empty)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel3, "关闭时间未填写！");
            return;
        }
        //首先检查开始日期，审批计划的开始日期一定要大于项目计划的结束日期（其实大于当前时间即可）
        if (DateTime.Parse(this.txtOpenDate.Value.Trim())<=DateTime.Now)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel3, "评审开启时间一定要大于系统时间！点击隐藏/显示查看时间");
            return;
        }
        //填充实体
        PM.Model.ApprovePlan aplan = new PM.Model.ApprovePlan();
        setModel(ref aplan);
        if (approve.Add(aplan) != -2)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel3, "审批计划已经成功添加！");
            this.ddlApprove.Items.Remove(ddlApprove.SelectedValue.Trim());
            this.txtOpenDate.Value = string.Empty;
            this.txtCloseDate.Value = string.Empty;
        }
    }
    //获取一个计划
    protected void ddlApprove_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlApprove.SelectedIndex != 0)
        {
            PM.Model.ProjectPlan aplan = new PM.Model.ProjectPlan();
            string plancode = ddlApprove.SelectedValue;
            aplan = approve.getPlanData(plancode);
            if (aplan != null)
            {
                lbplancode.Text = aplan.plancode;
                lbtype.Text = aplan.typename;
                lblevel.Text = aplan.plevel;
                lbptype.Text = aplan.ptype;
                lbopentime.Text = aplan.opendate.ToString();
                lbclosetime.Text = aplan.closedate.ToString();
                lbbuildtime.Text = aplan.builtdate.ToString();
                lbremark.Text = aplan.remark;
            }
        }
    }

    protected void setModel(ref PM.Model.ApprovePlan aplan)
    {
        aplan.approvecode = ddlApprove.SelectedValue.Trim().Replace("pdp", "app");//生成编号
        aplan.plancode = this.ddlApprove.SelectedValue.Trim();
        aplan.opentime = DateTime.Parse(this.txtOpenDate.Value.Trim());
        aplan.closetime = DateTime.Parse(this.txtCloseDate.Value.Trim());
        aplan.status = 1;
        aplan.operate = this.txtOperate.Text.Trim();
    }
}
